package com.zyf.project.capchain.listener.result.impl;

import com.zyf.project.capchain.listener.result.IResult;
import com.zyf.project.capchain.listener.state.State;
import lombok.Data;
import org.jetbrains.annotations.NotNull;

import java.io.Serializable;

/**
 * 业务层返回结果，将直接提交给前端
 *
 * @author Zhou Yifan
 */
@Data
public class Result<D> implements Serializable, IResult {

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 状态消息
     */
    private String message;

    /**
     * 是否操作成功
     */
    private Boolean isSuccess;

    /**
     * 操作数据
     */
    private transient D data;

    public Result() {
        this.code = 0;
        this.message = "";
        this.isSuccess = false;
    }

    /**
     * 设置Result相关属性
     *
     * @param state 状态类
     * @param data  数据
     */
    public void setResult(@NotNull State state, D data) {
        this.code = state.code();
        this.message = state.message();
        this.isSuccess = state.isSuccess();
        this.data = data;
    }
}
